﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Main_Terms_Conditions_Terms_Conditions : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    int ValTran = 0;
    string strsql = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["Mode"] == "New" || Request.QueryString["Mode"]==null)
            {
                
                BindGrid();
                btnSave.Text = "Save";
                pnlEdit.Visible = true;
            }
            if (Request.QueryString["Mode"] == "Edit")
            {
                BindData();
                btnSave.Text = "Update";
                pnlEdit.Visible = false;
            }
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        GetValidTran();
        if (Request.QueryString["Mode"] == "New")
        {
            InsertTermsConditions();
            Response.Redirect("~/Main/Terms_Conditions/Terms_Conditions.aspx?Mode=New");
        }
        else if (Request.QueryString["Mode"] == "Edit")
        {
            UpdateTermsConditions();
            Response.Redirect("~/Main/Terms_Conditions/Terms_Conditions.aspx?Mode=New");
        }
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["Mode"] == "New")
        {
            Response.Redirect("~/Welcome.aspx");
        }
        else if (Request.QueryString["Mode"] == "Edit")
        {
            Response.Redirect("~/Main/Terms_Conditions/Terms_Conditions.aspx?Mode=New");
        }
    }
    protected void GetValidTran()
    {
        if (chkSalesInvoice.Checked == true)
        {
            ValTran += 2;
        }
        if (chkSalesReturn.Checked == true)
        {
            ValTran += 8;
        }
        if (chkDCOutward.Checked == true)
        {
            ValTran += 4;
        }
        if (chkPurchaseReturn.Checked == true)
        {
            ValTran += 64;
        }
        hidTransaction.Value = ValTran.ToString();

    }
    protected void grdDisplay_SelectedIndexChanged(object sender, EventArgs e)
    {
        string key = grdDisplay.SelectedDataKey[0].ToString();
        Response.Redirect("~/Main/Terms_Conditions/Terms_Conditions.aspx?Mode=Edit&id=" + key.ToString() + "");
    }
    protected void BindGrid()
    {
        strsql = "Select * From ACC_TERMSCONDITIONS ORDER BY TRMDESC DESC";
        DataTable DTBind = DU.getdata(strsql);
        grdDisplay.DataSource = DTBind;
        grdDisplay.DataBind();
        custPager.TotalPages = 2; 
    }
    protected void InsertTermsConditions()
    {
        try
        {
            SqlParameter[] sparam =
                                  {
                                     new SqlParameter("@TRMDESC",txtTermsConditions.Text),
                                      new SqlParameter("@APPLICTOTRAN",hidTransaction.Value)
                                  };
            DU.executeprocedure("USP_ACC_SaveTermsAndConditions", sparam);
            lblMsg.Text = "inserted";
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
    protected void UpdateTermsConditions()
    {
        try
        {
            SqlParameter[] sparam =
                                  {
                                      new SqlParameter("@TRMID",Request.QueryString["id"].ToString()),
                                      new SqlParameter("@TRMDESC",txtTermsConditions.Text),
                                      new SqlParameter("@APPLICTOTRAN",hidTransaction.Value)
                                  };
            DU.executeprocedure("USP_ACC_UpdateTermsAndConditions", sparam);
            lblMsg.Text = "updated";
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
    protected void BindData()
    {
        if (Request.QueryString["id"] != null)
        {
            strsql = "Select * From ACC_TERMSCONDITIONS Where TRMID='" + Request.QueryString["id"].ToString() + "'";
            DataTable DTBind = DU.getdata(strsql);
            if (DTBind.Rows.Count > 0)
            {
                txtTermsConditions.Text = DTBind.Rows[0]["TRMDESC"].ToString();
                if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "2")
                {
                    chkSalesInvoice.Checked = true;
                }
                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "4")
                {
                    chkDCOutward.Checked = true;
                }
                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "8")
                {
                    chkSalesReturn.Checked = true;
                }
                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "64")
                {
                    chkPurchaseReturn.Checked = true;
                }

                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "6")
                {
                    chkSalesInvoice.Checked = true;
                    chkDCOutward.Checked = true;
                }
                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "10")
                {
                    chkSalesInvoice.Checked = true;
                    chkSalesReturn.Checked = true;
                }
                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "66")
                {
                    chkSalesInvoice.Checked = true;
                    chkPurchaseReturn.Checked = true;
                }
                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "12")
                {
                    chkDCOutward.Checked = true;
                    chkSalesReturn.Checked = true;
                }
                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "68")
                {
                    chkDCOutward.Checked = true;
                    chkPurchaseReturn.Checked = true;
                }
                else if (DTBind.Rows[0]["APPLICTOTRAN"].ToString() == "72")
                {
                    chkSalesReturn.Checked = true;
                    chkPurchaseReturn.Checked = true;
                }

            }
        }
    }
    protected void grdDisplay_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDisplay.PageIndex = e.NewPageIndex;
        BindGrid();

    }
   


    protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    CheckBox chkDisc = (CheckBox)e.Row.Cells[4].FindControl("chkDisc");
        //    bool chkVal = (bool)((DataRowView)(e.Row.DataItem)).Row.ItemArray[4];
        //    chkDisc.Checked = chkVal;
        //}
    }

   
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        grdDisplay.PageSize = e.CurrentPageSize;
        grdDisplay.PageIndex = e.CurrentPageNumber;
        BindGrid();
    }
    protected void grdDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
}

        


